/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.thienloc.omap.entity.report;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 *
 * @author GiangLT
 */
public class BroadcastProductionOfUser {

    private String username;
    private Boolean uniqueOperator = false;//nguoi dung duy nhat
    private Map<String, BroadcastProductionOfSyntax> syntaxMap = new TreeMap<String, BroadcastProductionOfSyntax>();

    public BroadcastProductionOfUser() {
//        try {
//            Properties properties = new Properties();
//            properties.load(new FileReader(Constant.CONFIG_FILE_PATH));
//            String[] syntaxOrders = properties.getProperty("syntaxOrder").split(",");
//            Map<String, Integer> syntaxOrderMap = Maps.newHashMapWithExpectedSize(syntaxOrders.length);
//            int idx = 0;
//            for (String syntax : syntaxOrders) {
//                syntaxOrderMap.put(syntax, ++idx);
//            }
//
//            SyntaxOrdering syntaxOrdering = new SyntaxOrdering(syntaxOrderMap);
//            syntaxMap = new TreeMap<String, BroadcastProductionOfSyntax>(syntaxOrdering);
//        } catch (Exception ex) {
//            Logger.getLogger(BroadcastProductionOfUser.class.getName()).log(Level.SEVERE, null, ex);
//        }
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public synchronized Integer getTotalProduction() {
        Integer result = 0;
        Collection<BroadcastProductionOfSyntax> productionOfSyntaxs = syntaxMap.values();

        for (BroadcastProductionOfSyntax productionOfSyntax : productionOfSyntaxs) {
            result += productionOfSyntax.getProduction();
        }
        return result;
    }

    public synchronized void addSyntax(BroadcastProductionOfSyntax bpos) {
        if (!syntaxMap.containsKey(bpos.getSyntax())) {
            syntaxMap.put(bpos.getSyntax(), bpos);
        }
    }

    public synchronized Integer increaseProduction(String syntax, Integer amount) {
        BroadcastProductionOfSyntax bpos = syntaxMap.get(syntax);
        if (bpos == null) {
            bpos = new BroadcastProductionOfSyntax(syntax, amount);
            syntaxMap.put(syntax, bpos);
        } else {
            bpos.setProduction(bpos.getProduction() + amount);
        }
        return bpos.getProduction();
    }

    public synchronized void setProduction(String syntax, Integer amount) {
        BroadcastProductionOfSyntax bpos = syntaxMap.get(syntax);
        bpos.setProduction(amount);
    }

    public boolean hasSyntax(String syntax) {
        return syntaxMap.containsKey(syntax);
    }

    public List<BroadcastProductionOfSyntax> getProductionOfSyntax() {
        return new ArrayList<BroadcastProductionOfSyntax>(syntaxMap.values());
    }

    public void addMessageToSyntax(String syntax, String message) {
        BroadcastProductionOfSyntax bpos = syntaxMap.get(syntax);
        bpos.addMessage(message);
    }

    public void setMdnSuffixRangeToSyntax(String syntax, Integer mdnSuffixStartRange, Integer mdnSuffixEndRange) {
        BroadcastProductionOfSyntax bpos = syntaxMap.get(syntax);
        bpos.setMdnSuffixStartRange(mdnSuffixStartRange);
        bpos.setMdnSuffixEndRange(mdnSuffixEndRange);
    }

    public Boolean getUniqueOperator() {
        return uniqueOperator;
    }

    public void setUniqueOperator(Boolean uniqueOperator) {
        this.uniqueOperator = uniqueOperator;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(username);
        sb.append(": ");
        for (BroadcastProductionOfSyntax bpos : getProductionOfSyntax()) {
            sb.append(bpos.getSyntax()).append("-").append(bpos.getProduction()).append(" | ");
        }
        return sb.toString();
    }
}
